/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reportcenter.auth.ui;

import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.dialog.CloseAction;
import org.springframework.richclient.dialog.ConfirmationDialog;
import org.springframework.richclient.dialog.FormBackedDialogPage;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;
import org.springframework.richclient.form.Form;
import org.springframework.util.Assert;
import reportcenter.auth.business.UserAuthorizationDao;
import reportcenter.auth.dominio.Users;

/**
 *
 * @author renan
 */
public class UserAuthPropertiesDialog extends TitledPageApplicationDialog{
    
    private Form form;
    private boolean creatingNew = false;
    private UserAuthorizationDao dao;
    
    public UserAuthPropertiesDialog(UserAuthorizationDao dao) {
        this(null, dao);
    }

    public UserAuthPropertiesDialog(Users user, UserAuthorizationDao dao) {
        Assert.notNull(dao, "Dao necessário para editar usuários");
        if (user == null) {
            creatingNew = true;
            user = new Users();
        }
        setCloseAction(CloseAction.DISPOSE);
        form = new UserAuthForm(user);
        setDialogPage(new FormBackedDialogPage(form));
        this.dao = dao;
    }
    
    private Users getEditingUser() {
        return (Users) form.getFormModel().getFormObject();
    }
    
    @Override
    protected void onAboutToShow() {
        if (creatingNew) {
            setTitle(getMessage("userAuthProperties.new.title"));
        } else {
            Users user = getEditingUser();
            String title = getMessage("userAuthProperties.edit.title", new Object[]{user.getUsername()});
            setTitle(title);
        }
    }

    @Override
    protected boolean onFinish() {
        // commit any buffered edits to the model
        form.getFormModel().commit();
        // Update the persistent store with the new/modified object.
        String eventType;
        if (creatingNew) {
            eventType = LifecycleApplicationEvent.CREATED;
            dao.add(getEditingUser());
            
        } else {
            eventType = LifecycleApplicationEvent.MODIFIED;
            dao.add(getEditingUser());
        }
        // And notify the rest of the application of the change
        getApplicationContext().publishEvent(new LifecycleApplicationEvent(eventType, getEditingUser()));
        return true;
    }
    
    protected void onCancel() {
        // Warn the user if they are about to discard their changes
        if (form.getFormModel().isDirty()) {
            String msg = getMessage("userAuthProperties.dirtyCancelMessage");
            String title = getMessage("userAuthProperties.dirtyCancelTitle");
            ConfirmationDialog dlg = new ConfirmationDialog(title, msg) {

                protected void onConfirm() {
                    UserAuthPropertiesDialog.super.onCancel();
                }
            };
            dlg.showDialog();
        } else {
            super.onCancel();
        }
    }   

}